package clock;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.Toolkit;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;


/**
 * Provides the interface behaviour for the interactive
 * ClickCounter.
 */

public class AlarmClockTranslation implements Translation, EventLabels {

	private AlarmClock alarmClock;
	private Presentation visibleInterface;
	private AlarmClockState theState;
	private PropertyChangeSupport listeners = new PropertyChangeSupport(this);

	private int timeCount = 0;
	private int flag = 0;
	private int stateTemp;
	private boolean buttonPressed = false;
	private int secondsCount = 0;
	private Time ticker;
	
	  /**
	   * This method provides this translation with a visible interface
	   * (presentation).
	   */
	
	public void setVisibleInterface(Presentation visibleInterface) {
	    this.visibleInterface = visibleInterface;
	    visibleInterface.setOffState();
	    fireUpdate(alarmClock.hrcountIs()  , alarmClock.mincountIs()  );
	    theState = OFF_STATE;
	} 
	
	public void setVisibleInterface1(Time ticker) {
	 	this.ticker = ticker;
	}
	  
	public void setApplication(AlarmClock alarmClock) {
	    this.alarmClock = alarmClock;
	}

	public void actionPerformed(ActionEvent event) {
		if(event.getActionCommand() == null) {
			timeCount++ ;
			if(timeCount == 60)	{
				timeCount = 0;
				alarmClock.countmin();
			}	
		}
		theState.actionPerformed( event );
	} 
	
	protected void changeState(AlarmClockState state) {
	    theState = state;
	}
	
	
	private interface AlarmClockState extends ActionListener {
    }
    
    private final AlarmClockState OFF_STATE = new AlarmClockState() {
        public void actionPerformed(ActionEvent event) {
          String actionOccured = event.getActionCommand();
          if(actionOccured == null) {
          	fireUpdate(alarmClock.hrcountIs()  , alarmClock.mincountIs()  );
          }
          if(ON.equals( actionOccured)) {
          	visibleInterface.setOnState() ;
          	fireUpdate(alarmClock.hrcountIs()  , alarmClock.mincountIs()  );
          	changeState(ON_STATE);
          }
          if(SET.equals( actionOccured)) {
          	visibleInterface.setSetState() ;
          	fireUpdate(alarmClock.alarmHrCountIs()  , alarmClock.alarmMinCountIs()  );
          	changeState(SET_STATE);
          }	
        }
        
     };
    
     private final AlarmClockState ON_STATE = new AlarmClockState() {
        public void actionPerformed(ActionEvent event) {
        	String actionOccured = event.getActionCommand();
        	if(actionOccured == null) {
        		fireUpdate(alarmClock.hrcountIs()  , alarmClock.mincountIs()  );
            }
        	else if(OFF.equals( actionOccured)) {
            	visibleInterface.setOffState() ;
            	fireUpdate(alarmClock.hrcountIs()  , alarmClock.mincountIs()  );
            	changeState(OFF_STATE);
            }
            else if(SET.equals( actionOccured)) {
            	visibleInterface.setSetState() ;
            	fireUpdate(alarmClock.alarmHrCountIs()   , alarmClock.alarmMinCountIs()  );
            	changeState(SET_STATE);
            }
            if(alarmClock.isAlarmTimeReached()) {
            	visibleInterface.setAlarmSoundState() ;
			    changeState(ALARM_SOUND_STATE) ;
			    Toolkit.getDefaultToolkit() .beep() ;
			}
    	}
     };
    
     private final AlarmClockState SET_STATE = new AlarmClockState() {
        public void actionPerformed(ActionEvent event) {
        	String actionOccured = event.getActionCommand();
        	if(ON.equals( actionOccured)) {
            	visibleInterface.setOnState() ;
            	fireUpdate(alarmClock.hrcountIs()  , alarmClock.mincountIs()  );
            	changeState(ON_STATE);
            }
            else if(OFF.equals( actionOccured)) {
            	visibleInterface.setOffState() ;
            	fireUpdate(alarmClock.hrcountIs()  , alarmClock.mincountIs()  );
            	changeState(OFF_STATE);
            }
            else if(HOUR.equals( actionOccured)) {
            	alarmClock.countAlarmHr() ;
            	fireUpdate(alarmClock.alarmHrCountIs()   , alarmClock.alarmMinCountIs()  );
            }
            else if(MINUTE.equals( actionOccured))  {
            	alarmClock.countAlarmMin() ;
            	fireUpdate(alarmClock.alarmHrCountIs()   , alarmClock.alarmMinCountIs()  );
            }
        }
     };
     
     private final AlarmClockState ALARM_DISPLAY_STATE = new AlarmClockState() {
        public void actionPerformed(ActionEvent event) {
        	fireUpdate(alarmClock.alarmHrCountIs()   , alarmClock.alarmMinCountIs()  );        
        }
     };
     
     private final AlarmClockState ALARM_SOUND_STATE = new AlarmClockState() {
        public void actionPerformed(ActionEvent event) {
          String actionOccured = event.getActionCommand();
          if(actionOccured == null) {
          	fireUpdate(alarmClock.hrcountIs()  , alarmClock.mincountIs()  );
          	Toolkit.getDefaultToolkit() .beep() ;
          }
          else if(HOUR.equals( actionOccured) || MINUTE.equals( actionOccured)) {
          	visibleInterface.setOnState() ;
          	fireUpdate(alarmClock.hrcountIs()  , alarmClock.mincountIs()  );
          	changeState(ON_STATE);
          }
          else if(OFF.equals( actionOccured)) {
          	visibleInterface.setOffState() ;
          	fireUpdate(alarmClock.hrcountIs()  , alarmClock.mincountIs()  );
          	changeState(OFF_STATE);
          }
          else if(SET.equals( actionOccured)) {
          	visibleInterface.setSetState() ;
          	fireUpdate(alarmClock.alarmHrCountIs()   , alarmClock.alarmMinCountIs()  );
          	changeState(SET_STATE);
          }
        }
     };

	
	public void mouseClicked(MouseEvent arg0) {
	}

	public void mouseEntered(MouseEvent arg0) {
	}

	public void mouseExited(MouseEvent arg0) {
	}

	public void mousePressed(MouseEvent arg0) {
		buttonPressed = true;
    	if(theState == ON_STATE || theState == OFF_STATE) {
        	visibleInterface.setAlarmDisplayState() ;
        	fireUpdate(alarmClock.alarmHrCountIs()   , alarmClock.alarmMinCountIs()  );
			changeState(ALARM_DISPLAY_STATE);
    	}
	}

	public void mouseReleased(MouseEvent arg0) {
		buttonPressed = false;
    	if(theState == ALARM_DISPLAY_STATE) {
    		visibleInterface.setOnState() ;
    	   	fireUpdate(alarmClock.hrcountIs()  , alarmClock.mincountIs()  );
    	   	changeState(ON_STATE);
    	}   	
	}
	
	public synchronized void addPropertyChangeListener(PropertyChangeListener l) {
	    listeners.addPropertyChangeListener(l);
	}

	public synchronized void removePropertyChangeListener(PropertyChangeListener l) {
	    listeners.removePropertyChangeListener(l);
	}

	private int hourT = -1;
	private int minT = -1;
	protected void fireUpdate(int hour, int min) {
		if(hour != hourT) {
			listeners.firePropertyChange(new PropertyChangeEvent(this, "time", null, hour + "," + min));
			hourT = hour;
		}	
		else if(min != minT) {
			listeners.firePropertyChange(new PropertyChangeEvent(this, "time", null, hour + "," + min));	
			minT = min;
		}
	}
  
} 
